package cn.fl.capital.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.capital.condition.CapAdvFundQueryCondition;
import cn.fl.capital.dto.CapAdvFundRvtDTO;
import cn.fl.capital.service.ICapAdvFundApiService;
import cn.fl.capital.service.ICapAdvFundRvtService;
import cn.fl.capital.transform.CapAdvFundRvtVOTransform;
import cn.fl.capital.vo.CapAdvFundRvtVO;
import cn.fl.capital.vo.CapAdvFundVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 垫款资金流水表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapAdvFundRvtServiceImpl implements ICapAdvFundRvtService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapAdvFundRvtServiceImpl.class);

    /**
     * 垫款资金流水表 暴露服务
     */
    @Autowired
    private ICapAdvFundApiService capAdvFundApiService;
    /**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        CapAdvFundQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new CapAdvFundQueryCondition());
        }
        condition = (CapAdvFundQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.capAdvFundApiService.searchCapAdvFundRvtPayListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<CapAdvFundRvtVO> voList = CapAdvFundRvtVOTransform.toVOList((List<CapAdvFundRvtDTO>) page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    /**
     * 分页查询所有  查询待冲销付款列表
     *
     * @param page
     * @return
     */
    @Override
    public Pager searchRvtFundListPage(Pager page) throws BaseException {
        CapAdvFundQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new CapAdvFundQueryCondition());
        }
        condition = (CapAdvFundQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.capAdvFundApiService.searchRvtFundListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<CapAdvFundRvtVO> voList = CapAdvFundRvtVOTransform.toVOList((List<CapAdvFundRvtDTO>) page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    /**
     * 分页查询所有  查询待冲销支付表列表
     *
     * @param page
     * @return
     */
    @Override
    public Pager searchRvtPaySchListPage(Pager page) throws BaseException {
        CapAdvFundQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new CapAdvFundQueryCondition());
        }
        condition = (CapAdvFundQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.capAdvFundApiService.searchRvtPaySchListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<CapAdvFundRvtVO> voList = CapAdvFundRvtVOTransform.toVOList((List<CapAdvFundRvtDTO>) page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }


    /**
     * 批量新增冲销垫款资金及核销明细
     *
     * @param condition
     * @return
     */
    @Override
    public DataResultVO batchAddRvtFund(CapAdvFundQueryCondition condition) throws BaseException {
        if (null == condition) {
            condition = new CapAdvFundQueryCondition();
        }
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());

        try {
            DataResultDTO resultDTO = this.capAdvFundApiService.batchAddRvtFund(condition);
            if (null == resultDTO) {
                BaseLogUtils.info(logger,"batchAddRvtFund","生成并发送付款指令返回==>空",CurrentThreadContext.getCurrentUserName());
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "生成并发送付款指令异常");
            } else {
                BaseLogUtils.info(logger,"batchAddRvtFund","生成并发送付款指令返回==>"+ JSON.toJSONString(resultDTO),CurrentThreadContext.getCurrentUserName());
                return new DataResultVO(Integer.parseInt(resultDTO.getCode()), resultDTO.getInfo(), resultDTO.getData());
            }
        } catch (BaseException e) {
            BaseLogUtils.info(logger,"batchAddRvtFund","生成并发送付款指令异常BaseException",e);
        } catch (NumberFormatException e) {
            BaseLogUtils.info(logger,"batchAddRvtFund","生成并发送付款指令异常NumberFormatException",e);
        } catch (Exception e) {
            BaseLogUtils.info(logger,"batchAddRvtFund","生成并发送付款指令异常Exception",e);
        }
        return new DataResultVO(BaseConstant.IS_YESNO_NO, "生成并发送付款指令异常");
    }

    /**
     * 添加数据字典名称
     *
     * @param voList
     */
    public void setDictData(List<CapAdvFundVO> voList) {
        if (CollectionUtils.isEmpty(voList)) return;
        /*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapAdvFundConstant.CODE_CD);
		for(CapAdvFundVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
    }

    /**
     * 查询数据字典
     *
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd) {
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

}
